<script setup>
import { watch, onMounted, onBeforeUnmount } from 'vue';

const props = defineProps({
	camera: {
		type: Object,
		required: true,
	},
  isEnlarge: {
    type: Boolean,
    default: false,
  }
})

var player = null;
onMounted(() => {
  if (!window.EZUIKit) {
    let e = document.createElement('script');
    e.setAttribute('type', 'text/JavaScript');
    e.setAttribute('src', '/ezuikit_static/ezuikit.js');
    e.onload = function () {
      reloadVideo();
    };
    document.body.appendChild(e);
  } else {
    setTimeout(() => {
      reloadVideo();
    }, 200)
  }
});

watch(() => props.camera.id, (nv, ov) => {
  if (nv) {
    const destroyPromise = player.destroy();
    destroyPromise.then(() => {
      reloadVideo();
    });
  }
});
watch(() => props.isEnlarge, (nv, ov) => {
  const destroyPromise = player.destroy();
  destroyPromise.then(() => {
    reloadVideo();
  });
});

function reloadVideo() {
	let appKey = "49b7a0c12a1b4af29121521651ad164f", appSecret = "9ee3f86fbadf7a1ae6c9be0377a31f2c";
  
	fetch("https://open.ys7.com/api/lapp/token/get", {
		method: "post",
		body: `appKey=${appKey}&appSecret=${appSecret}`,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		}
	})
	  .then(response => response.json())
	  .then(res => {
			let accessToken = res.data.accessToken;
			player = new EZUIKit.EZUIKitPlayer({
			  id: 'video-ezui-container', // 视频容器ID
				accessToken,
        url: props.camera.addr,
        width: props.isEnlarge ? 1110 : 366,
        height: props.isEnlarge ? 655 : 251,
			  // url: 'ezopen://open.ys7.com/AW5687315/1.live',
			  // simple - 极简版; pcLive-pc直播；pcRec-pc回放；mobileLive-移动端直播；mobileRec-移动端回放;security - 安防版;voice-语音版;
			  // template: 'simple',
			  // plugin: ['talk'], // 加载插件，talk-对讲
			});
      window.player = player;
	  });
}

function play() {
	var playPromise = player.play();
	playPromise.then((data) => {
		console.log("promise 获取 数据", data)
	});
}
function stop() {
	var stopPromise = player.stop();
	stopPromise.then((data) => {
	  console.log("promise 获取 数据", data)
	});
}
function getOSDTime() {
	var getOSDTimePromise = player.getOSDTime();
	getOSDTimePromise.then((data) => {
	  console.log("promise 获取 数据", data)
	});
}
function capturePicture() {
	var capturePicturePromise = player.capturePicture(`${new Date().getTime()}`);
	capturePicturePromise.then((data) => {
	  console.log("promise 获取 数据", data)
	});
}
function openSound() {
	var openSoundPromise = player.openSound();
	openSoundPromise.then((data) => {
	  console.log("promise 获取 数据", data)
	});
}
function closeSound() {
	var openSoundPromise = player.closeSound();
	openSoundPromise.then((data) => {
	  console.log("promise 获取 数据", data)
	});
}
function startSave() {
	var startSavePromise = player.startSave(`${new Date().getTime()}`);
	startSavePromise.then((data) => {
	  console.log("promise 获取 数据", data)
	});
}
function stopSave() {
	var stopSavePromise = player.stopSave();
	stopSavePromise.then((data) => {
	  console.log("promise 获取 数据", data)
	});
}
function ezopenStartTalk() {
	player.startTalk();
}
function ezopenStopTalk() {
	player.stopTalk();
}
function fullScreen() {
	player.fullScreen();
}
function destroy() {
	var destroyPromise = player.destroy();
	destroyPromise.then((data) => {
	  console.log("promise 获取 数据", data)
	});
}

onBeforeUnmount(() => {
  player.destroy();
});
</script>



<template>
  <div id="video-ezui-container"></div>
  <div>
    <!-- <button v-on:click="stop">stop</button> -->
    <!-- <button v-on:click="play">play</button> -->
    <!-- <button v-on:click="openSound">openSound</button>
    <button v-on:click="closeSound">closeSound</button>
    <button v-on:click="startSave">startSave</button>
    <button v-on:click="stopSave">stopSave</button>
    <button v-on:click="capturePicture">capturePicture</button>
    <button v-on:click="fullScreen">fullScreen</button>
    <button v-on:click="getOSDTime">getOSDTime</button>
    <button v-on:click="ezopenStartTalk">开始对讲</button>
    <button v-on:click="ezopenStopTalk">结束对讲</button>
    <button v-on:click="destroy">销毁</button> -->
  </div>
</template>

<style scoped>
#video-ezui-container {
  width: 100%;
  height: 100%;
}
</style>

